#!/bin/csh
#Pass name of PRM file to clean. Otherwise stdin will be read.
#Add -f as the second parameter to force overwritting the original PRM file. 

echo Deprecated! use instead: 
echo "tisc -f projectname > newprojectname.PRM"

set inputPRM = $1 #$tisc_dir/doc/template.PRM
awk 'BEGIN{field1length=15}\
	{\
		field1=$1;\
		field2=$2;\
		if (field1=="#(END") exit;\
		if (substr($0,1,1)=="#") print $0;\
		if (substr($1,1,1)=="#") next;\
		if ($0=="") {print; next;}\
		if (field1=="switch_verbose") 	field1="verbose_level"; \
		if (field1=="switch_debug") next;\
		if (field1=="lith_type") 	field1="isost_model"; \
		if (field1=="erosed_type") 	field1="erosed_model"; \
		if (field1=="switch_hydro") 	field1="hydro_model"; \
		if (field1=="leng_fluv_eros") 	field1="erodability"; \
		if (field1=="leng_fluv_sedim") 	field1="l_fluv_sedim"; \
		if (field1=="switch_erosed") 	field1="erosed_model"; \
		if (field1=="l_fluv_eros") 	field1="erodability"; \
		if (field1=="l_fluv_eros_sed") 	field1="erodability_sed"; \
		if (field1=="dtmemounit") 	field1="dt_record"; \
		if (field1=="alt0")      	field1="zini"; \
		blankspaces="";\
		if (field1== "version" || field2 ~ /^[-+]?([0-9]+\.?|\.[0-9])[0-9]*([eE][-+]?[0-9]+)?$/) {\
			nblankspaces=field1length-length(field1); for (i=0;i<nblankspaces;i++) blankspaces=sprintf("%s ", blankspaces); \
			printf("%s%s\t%s\n", field1, blankspaces, field2, nblankspaces, blankspaces); } \
	}' $inputPRM > tmp.cleanPRM.tmp

if ("$2" == "-f") then
	mv tmp.cleanPRM.tmp $inputPRM
	echo File has been cleaned and replaced.
else
	cat tmp.cleanPRM.tmp
	rm tmp.cleanPRM.tmp
endif

